package rus.sq.osTicket.Mobile;

import android.util.Xml;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class StackOverflowXmlParser {
    private static final String ns = null;
    public List<Ticket> parse(InputStream inputStream) throws XmlPullParserException, IOException{
        try{
            XmlPullParser xmlPullParser = Xml.newPullParser();
            xmlPullParser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES,false);
            xmlPullParser.setInput(inputStream,null);
            xmlPullParser.nextTag();
            return readFeed(xmlPullParser);
        }
        finally {
            inputStream.close();
        }
    }

    private List<Ticket> readFeed(XmlPullParser xmlPullParser) throws XmlPullParserException,IOException {
        List<Ticket> ticketList = new ArrayList<Ticket>();
        xmlPullParser.require(XmlPullParser.START_TAG,ns,"resultset");

        while (xmlPullParser.next() != XmlPullParser.END_DOCUMENT){
            if (xmlPullParser.getEventType() != XmlPullParser.START_TAG) {
                continue;
            }
            String name = xmlPullParser.getName();
            if (name.equals("row")){
                ticketList.add(readEntry(xmlPullParser));
                //entries.add(readEntry(xmlPullParser));
            }
            else {
                skip(xmlPullParser);
            }
        }
        return ticketList;
    }

    private Ticket readEntry(XmlPullParser xmlPullParser) throws XmlPullParserException, IOException{
        xmlPullParser.require(XmlPullParser.START_TAG,ns,"row");
        int ticket_id = 0,number = 0,user_id = 0,user_email_id = 0,dept_id = 0,sla_id  = 0,topic_id = 0,staff_id = 0,
                team_id = 0,email_id = 0;
        String ip_address = "" ,status = "" ,source = "";
        boolean isoverdue = false,isanswered = false;
        Date duedate = null,reopened = null,closed= null,lastmessage= null,lastresponse= null,created= null,
                updated= null;

        while (xmlPullParser.next() != XmlPullParser.END_TAG){
            if (xmlPullParser.getEventType() != XmlPullParser.START_TAG){
                continue;
            }
            xmlPullParser.require(XmlPullParser.START_TAG, ns, "field");
            String tag = xmlPullParser.getName();

            if (tag.equals("field")){
                String name = xmlPullParser.getAttributeValue(null,"name");
                if (name.equals("ticket_id")){
                    ticket_id = Integer.parseInt(xmlPullParser.getAttributeValue(null,"ticket_id"));
                }else if (name.equals("number")){

                }else if (name.equals("user_id")){

                }else if (name.equals("user_email_id")){

                }else if (name.equals("dept_id")){

                }else if (name.equals("sla_id")){

                }else if (name.equals("topic_id")){

                }else if (name.equals("staff_id")){

                }else if (name.equals("team_id")){

                }else if (name.equals("email_id")){

                }else if (name.equals("ip_address")){

                }
                xmlPullParser.nextTag();
            }
        }
        return new Ticket(ticket_id,number,user_id,user_email_id,dept_id,sla_id,topic_id,staff_id,team_id,email_id,ip_address,status,source,isoverdue ,isanswered, duedate,reopened,closed,lastmessage,lastresponse,created,updated);
    }

    private void skip(XmlPullParser xmlPullParser) {

    }

}
